<?php
// +----------------------------------------------------------------------
// | snakeGC
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: GodConfident <godconfident@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\NodeModel;

class Node extends Base
{
    // 节点列表
    public function index()
    {
        if (request()->isAjax()) {
            $node = new NodeModel();
            $nodes = $node->getNodeList();
            $nodes = getTree(objToArray($nodes), false);
            $nodea = $this->node_id($nodes);
            return json(msgs(1, $nodea, 'ok'));
        }
        return $this->fetch();
    }
    
    //节点 _id 处理
    public function node_id($nodes)
    {
        foreach ($nodes as $k => &$v) {
            $nodes[$k]['id'] = $nodes[$k]['_id']['$oid'];
            if (!empty($v['children'])) {
                $v['children'] = $this->node_id($v['children']);
            }
        }
        return $nodes;
    }
    
    // 添加节点
    public function nodeAdd()
    {
        $param = input('post.');
        $node = new NodeModel();
        $flag = $node->insertNode($param);
        $this->removRoleCache();
        return json(msgs($flag['code'], $flag['data'], $flag['msg']));
    }
    
    // 编辑节点
    public function nodeEdit()
    {
        $param = input('post.');
        // var_dump($param);die;
        $node = new NodeModel();
        $flag = $node->editNode($param);
        $this->removRoleCache();
        return json(msgs($flag['code'], $flag['data'], $flag['msg']));
    }
    
    // 删除节点
    public function nodeDel()
    {
        $id = input('param.id');
        $role = new NodeModel();
        $flag = $role->delNode($id);
        $this->removRoleCache();
        return json(msgs($flag['code'], $flag['data'], $flag['msg']));
    }
}
